package com.msxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msxt.dto.YudingxinxiEntity;
import com.msxt.result.R;
import com.msxt.service.YudingxinxiService;
import com.msxt.utils.MPUtil;
import com.msxt.utils.PageUtils;
import com.msxt.view.YudingxinxiView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 预定信息
 * 后端接口
 */
@RestController
@RequestMapping("/yudingxinxi")
public class YudingxinxiController {
	@Autowired
	private YudingxinxiService yudingxinxiService;

	/**
	 * 后端列表
	 */
	@GetMapping("/page")
	public R page(@RequestParam Map<String, Object> params,
				  YudingxinxiEntity yudingxinxi,
				  HttpServletRequest request) {
		// 从请求属性获取 tableName 和 username
		String tableName = (String) request.getAttribute("tableName");
		String username = (String) request.getAttribute("username");

		if (tableName != null && username != null) {
			if ("shangjia".equals(tableName)) {
				yudingxinxi.setShangjiazhanghao(username);
			} else if ("yonghu".equals(tableName)) {
				yudingxinxi.setYonghuzhanghao(username);
			}
		}

		QueryWrapper<YudingxinxiEntity> wrapper = new QueryWrapper<>();
		PageUtils page = yudingxinxiService.queryPage(params,
				MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(wrapper, yudingxinxi), params), params));
		return R.ok().put("data", page);
	}

	/**
	 * 前端列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params,
				  YudingxinxiEntity yudingxinxi,
				  HttpServletRequest request) {
		QueryWrapper<YudingxinxiEntity> wrapper = new QueryWrapper<>();
		PageUtils page = yudingxinxiService.queryPage(params,
				MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(wrapper, yudingxinxi), params), params));
		return R.ok().put("data", page);
	}

	/**
	 * 列表
	 */
	@GetMapping("/lists")
	public R list(YudingxinxiEntity yudingxinxi) {
		QueryWrapper<YudingxinxiEntity> wrapper = new QueryWrapper<>();
		wrapper.allEq(MPUtil.allEQMapPre(yudingxinxi, "yudingxinxi"));
		return R.ok().put("data", yudingxinxiService.list(wrapper));
	}

	/**
	 * 查询
	 */
	@GetMapping("/query")
	public R query(YudingxinxiEntity yudingxinxi) {
		QueryWrapper<YudingxinxiEntity> wrapper = new QueryWrapper<>();
		wrapper.allEq(MPUtil.allEQMapPre(yudingxinxi, "yudingxinxi"));
		YudingxinxiView yudingxinxiView = yudingxinxiService.selectView(wrapper);
		return R.ok("查询预定信息成功").put("data", yudingxinxiView);
	}

	/**
	 * 后端详情
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {
		YudingxinxiEntity yudingxinxi = yudingxinxiService.getById(id);
		return R.ok().put("data", yudingxinxi);
	}

	/**
	 * 前端详情
	 */
	@GetMapping("/detail/{id}")
	public R detail(@PathVariable("id") Long id) {
		YudingxinxiEntity yudingxinxi = yudingxinxiService.getById(id);
		return R.ok().put("data", yudingxinxi);
	}

	/**
	 * 后端保存
	 */
	@PostMapping("/save")
	public R save(@RequestBody YudingxinxiEntity yudingxinxi,
				  HttpServletRequest request) {
		yudingxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
		yudingxinxiService.save(yudingxinxi);
		return R.ok();
	}

	/**
	 * 前端保存
	 */
	@PostMapping("/add")
	public R add(@RequestBody YudingxinxiEntity yudingxinxi,
				 HttpServletRequest request) {
		yudingxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
		yudingxinxiService.save(yudingxinxi);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@Transactional
	public R update(@RequestBody YudingxinxiEntity yudingxinxi,
					HttpServletRequest request) {
		yudingxinxiService.updateById(yudingxinxi);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] ids) {
		yudingxinxiService.removeByIds(Arrays.asList(ids));
		return R.ok();
	}

	/**
	 * 提醒接口
	 */
	@GetMapping("/remind/{columnName}/{type}")
	public R remindCount(@PathVariable("columnName") String columnName,
						 HttpServletRequest request,
						 @PathVariable("type") String type,
						 @RequestParam Map<String, Object> map) {
		map.put("column", columnName);
		map.put("type", type);

		if("2".equals(type)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date remindStartDate = null;
			Date remindEndDate = null;
			if(map.get("remindstart") != null) {
				Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH, remindStart);
				remindStartDate = c.getTime();
				map.put("remindstart", sdf.format(remindStartDate));
			}
			if(map.get("remindend") != null) {
				Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH, remindEnd);
				remindEndDate = c.getTime();
				map.put("remindend", sdf.format(remindEndDate));
			}
		}

		QueryWrapper<YudingxinxiEntity> wrapper = new QueryWrapper<>();
		if(map.get("remindstart") != null) {
			wrapper.ge(columnName, map.get("remindstart"));
		}
		if(map.get("remindend") != null) {
			wrapper.le(columnName, map.get("remindend"));
		}

		// 从请求属性获取 tableName 和 username
		String tableName = (String) request.getAttribute("tableName");
		String username = (String) request.getAttribute("username");

		if (tableName != null && username != null) {
			if ("shangjia".equals(tableName)) {
				wrapper.eq("shangjiazhanghao", username);
			}
			if ("yonghu".equals(tableName)) {
				wrapper.eq("yonghuzhanghao", username);
			}
		}

		long count = yudingxinxiService.count(wrapper);
		return R.ok().put("count", count);
	}
}